{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ca251904",
   "metadata": {
    "_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
    "_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
    "execution": {
     "iopub.execute_input": "2024-10-20T16:21:28.267642Z",
     "iopub.status.busy": "2024-10-20T16:21:28.267092Z",
     "iopub.status.idle": "2024-10-20T16:21:28.458352Z",
     "shell.execute_reply": "2024-10-20T16:21:28.457054Z"
    },
    "papermill": {
     "duration": 0.198872,
     "end_time": "2024-10-20T16:21:28.460985",
     "exception": false,
     "start_time": "2024-10-20T16:21:28.262113",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import glob\n",
    "import pickle\n",
    "import math\n",
    "\n",
    "import numpy as np\n",
    "from tqdm.auto import tqdm\n",
    "from joblib import Parallel, delayed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9ec2122c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-20T16:21:28.468688Z",
     "iopub.status.busy": "2024-10-20T16:21:28.468285Z",
     "iopub.status.idle": "2024-10-20T16:21:28.578533Z",
     "shell.execute_reply": "2024-10-20T16:21:28.577325Z"
    },
    "papermill": {
     "duration": 0.117549,
     "end_time": "2024-10-20T16:21:28.581391",
     "exception": false,
     "start_time": "2024-10-20T16:21:28.463842",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "file_pat = \"/kaggle/input/ppg-dalia-dataset/PPG_FieldStudy/S*/S*.pkl\"\n",
    "filepaths = glob.glob(file_pat)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6c07da7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-20T16:21:28.588402Z",
     "iopub.status.busy": "2024-10-20T16:21:28.587961Z",
     "iopub.status.idle": "2024-10-20T16:21:49.705546Z",
     "shell.execute_reply": "2024-10-20T16:21:49.704195Z"
    },
    "papermill": {
     "duration": 21.124031,
     "end_time": "2024-10-20T16:21:49.708169",
     "exception": false,
     "start_time": "2024-10-20T16:21:28.584138",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open(filepaths[0], \"rb\") as f:\n",
    "    data = pickle.load(f, encoding = \"latin-1\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1632e61f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-20T16:21:49.715469Z",
     "iopub.status.busy": "2024-10-20T16:21:49.714995Z",
     "iopub.status.idle": "2024-10-20T16:21:49.721279Z",
     "shell.execute_reply": "2024-10-20T16:21:49.720034Z"
    },
    "papermill": {
     "duration": 0.013121,
     "end_time": "2024-10-20T16:21:49.723990",
     "exception": false,
     "start_time": "2024-10-20T16:21:49.710869",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['rpeaks', 'signal', 'label', 'activity', 'questionnaire', 'subject'])\n",
      "35832\n"
     ]
    }
   ],
   "source": [
    "print(data.keys())\n",
    "activities = data[\"activity\"]\n",
    "print(len(activities))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b2b87fd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-20T16:21:49.731507Z",
     "iopub.status.busy": "2024-10-20T16:21:49.731102Z",
     "iopub.status.idle": "2024-10-20T16:28:26.335682Z",
     "shell.execute_reply": "2024-10-20T16:28:26.334315Z"
    },
    "papermill": {
     "duration": 396.616297,
     "end_time": "2024-10-20T16:28:26.343313",
     "exception": false,
     "start_time": "2024-10-20T16:21:49.727016",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7a3136871380472ea39e04a5262ad00d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/15 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n",
      "ecg\n",
      "ppg\n",
      "acc\n",
      "label\n",
      "activity\n"
     ]
    }
   ],
   "source": [
    "for path in tqdm(filepaths):\n",
    "# for path in filepaths:\n",
    "    subject = path.split(\"/\")[-2]\n",
    "\n",
    "    with open(path, \"rb\") as f:\n",
    "        data = pickle.load(f, encoding=\"latin-1\")\n",
    "        \n",
    "    ecg = data[\"signal\"][\"chest\"][\"ECG\"].astype(np.float32).flatten()\n",
    "    ppg = data[\"signal\"][\"wrist\"][\"BVP\"].astype(np.float32).flatten()\n",
    "    acc = data[\"signal\"][\"wrist\"][\"ACC\"].astype(np.float32)\n",
    "    acc = np.transpose(acc, (1, 0))\n",
    "    \n",
    "    activities = data[\"activity\"].astype(np.float32).flatten()\n",
    "    label = data[\"label\"].astype(np.float32).flatten()\n",
    "    \n",
    "    num_labels = label.shape[0]\n",
    "\n",
    "    freq_s = 700\n",
    "    ecg_chunks = []\n",
    "    assert math.floor(ecg.shape[0] / 700 / 2 - 3) == num_labels\n",
    "\n",
    "    for i in range(num_labels):\n",
    "        start = i * freq_s * 2\n",
    "        end = start + 8 * freq_s\n",
    "        ecg_chunks.append(ecg[start:end])\n",
    "\n",
    "    freq_s = 64\n",
    "    ppg_chunks = []\n",
    "    assert math.floor(ppg.shape[0] / 64 / 2 - 3) == num_labels\n",
    "\n",
    "    for i in range(num_labels):\n",
    "        start = i * freq_s * 2\n",
    "        end = start + 8 * freq_s\n",
    "        ppg_chunks.append(ppg[start:end])\n",
    "\n",
    "    freq_acc = 32\n",
    "    acc_chunks = []\n",
    "    assert math.floor(acc.shape[1] / 32 / 2 - 3) == num_labels\n",
    "    for i in range(num_labels):\n",
    "        start = i*freq_acc * 2\n",
    "        end =  start + 8 * freq_acc\n",
    "        acc_chunks.append(acc[:,start:end])\n",
    "    \n",
    "    \n",
    "    \n",
    "    freq_act = 4\n",
    "    activity_chunks = []\n",
    "    assert math.floor(activities.shape[0] / 4 / 2 - 3) == num_labels\n",
    "    for i in range(num_labels):\n",
    "        start = i * freq_act * 2\n",
    "        end = start + 8 * freq_act\n",
    "        activity_chunks.append(activities[start:end])\n",
    "    \n",
    "    \n",
    "    \n",
    "\n",
    "    ecg_chunks = np.vstack(ecg_chunks)\n",
    "    ppg_chunks = np.vstack(ppg_chunks)\n",
    "    acc_chunks  = np.stack(acc_chunks, axis=0)\n",
    "    activity_chunks = np.vstack(activity_chunks)\n",
    "    \n",
    "    np.savez_compressed(subject, ecg=ecg_chunks, ppg=ppg_chunks,acc=acc_chunks, label=label, activity = activity_chunks)\n",
    "    loaded = np.load(subject+'.npz', mmap_mode='r')\n",
    "    for k in loaded.files:\n",
    "        print(k)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "717854c6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-10-20T16:28:26.354506Z",
     "iopub.status.busy": "2024-10-20T16:28:26.354018Z",
     "iopub.status.idle": "2024-10-20T16:28:26.364210Z",
     "shell.execute_reply": "2024-10-20T16:28:26.363041Z"
    },
    "papermill": {
     "duration": 0.018891,
     "end_time": "2024-10-20T16:28:26.366622",
     "exception": false,
     "start_time": "2024-10-20T16:28:26.347731",
     "status": "completed"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3954, 3, 256)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "acc_chunks.shape"
   ]
  }
 ],
 "metadata": {
  "kaggle": {
   "accelerator": "none",
   "dataSources": [
    {
     "datasetId": 5711388,
     "sourceId": 9406793,
     "sourceType": "datasetVersion"
    }
   ],
   "dockerImageVersionId": 30761,
   "isGpuEnabled": false,
   "isInternetEnabled": true,
   "language": "python",
   "sourceType": "notebook"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 421.634248,
   "end_time": "2024-10-20T16:28:26.894407",
   "environment_variables": {},
   "exception": null,
   "input_path": "__notebook__.ipynb",
   "output_path": "__notebook__.ipynb",
   "parameters": {},
   "start_time": "2024-10-20T16:21:25.260159",
   "version": "2.6.0"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {
     "1c8ff8b0649146c4b3b4869fe7e39009": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_2dec09f2fa474e49980b5954b0a79b9e",
       "placeholder": "​",
       "style": "IPY_MODEL_dbc8e1d38cc54b7ba3f111fe19756a9e",
       "value": "100%"
      }
     },
     "2806fb516e5345baa8bed972e7426ef9": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "2dec09f2fa474e49980b5954b0a79b9e": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "34f1faa862484b53acbeb75d1a1aa114": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "40215108511f4fb3a32b996cee54e10d": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "FloatProgressModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "FloatProgressModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "ProgressView",
       "bar_style": "success",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_2806fb516e5345baa8bed972e7426ef9",
       "max": 15.0,
       "min": 0.0,
       "orientation": "horizontal",
       "style": "IPY_MODEL_c2a8296f19da4234a767c6d07a35dbd2",
       "value": 15.0
      }
     },
     "4a88df612095416384aa1f7f182960fa": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "DescriptionStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "description_width": ""
      }
     },
     "5348b7ded0654b73af5b8d83d3cd40e9": {
      "model_module": "@jupyter-widgets/base",
      "model_module_version": "1.2.0",
      "model_name": "LayoutModel",
      "state": {
       "_model_module": "@jupyter-widgets/base",
       "_model_module_version": "1.2.0",
       "_model_name": "LayoutModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "LayoutView",
       "align_content": null,
       "align_items": null,
       "align_self": null,
       "border": null,
       "bottom": null,
       "display": null,
       "flex": null,
       "flex_flow": null,
       "grid_area": null,
       "grid_auto_columns": null,
       "grid_auto_flow": null,
       "grid_auto_rows": null,
       "grid_column": null,
       "grid_gap": null,
       "grid_row": null,
       "grid_template_areas": null,
       "grid_template_columns": null,
       "grid_template_rows": null,
       "height": null,
       "justify_content": null,
       "justify_items": null,
       "left": null,
       "margin": null,
       "max_height": null,
       "max_width": null,
       "min_height": null,
       "min_width": null,
       "object_fit": null,
       "object_position": null,
       "order": null,
       "overflow": null,
       "overflow_x": null,
       "overflow_y": null,
       "padding": null,
       "right": null,
       "top": null,
       "visibility": null,
       "width": null
      }
     },
     "7a3136871380472ea39e04a5262ad00d": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HBoxModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HBoxModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HBoxView",
       "box_style": "",
       "children": [
        "IPY_MODEL_1c8ff8b0649146c4b3b4869fe7e39009",
        "IPY_MODEL_40215108511f4fb3a32b996cee54e10d",
        "IPY_MODEL_f3b3be12684b447cabf9d7c1a9897ba7"
       ],
       "layout": "IPY_MODEL_34f1faa862484b53acbeb75d1a1aa114"
      }
     },
     "c2a8296f19da4234a767c6d07a35dbd2": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "ProgressStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "ProgressStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "bar_color": null,
       "description_width": ""
      }
     },
     "dbc8e1d38cc54b7ba3f111fe19756a9e": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "DescriptionStyleModel",
      "state": {
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "DescriptionStyleModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/base",
       "_view_module_version": "1.2.0",
       "_view_name": "StyleView",
       "description_width": ""
      }
     },
     "f3b3be12684b447cabf9d7c1a9897ba7": {
      "model_module": "@jupyter-widgets/controls",
      "model_module_version": "1.5.0",
      "model_name": "HTMLModel",
      "state": {
       "_dom_classes": [],
       "_model_module": "@jupyter-widgets/controls",
       "_model_module_version": "1.5.0",
       "_model_name": "HTMLModel",
       "_view_count": null,
       "_view_module": "@jupyter-widgets/controls",
       "_view_module_version": "1.5.0",
       "_view_name": "HTMLView",
       "description": "",
       "description_tooltip": null,
       "layout": "IPY_MODEL_5348b7ded0654b73af5b8d83d3cd40e9",
       "placeholder": "​",
       "style": "IPY_MODEL_4a88df612095416384aa1f7f182960fa",
       "value": " 15/15 [06:36&lt;00:00, 25.15s/it]"
      }
     }
    },
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
